package com.pan.service.aspect;


import com.pan.entity.Result;
import com.pan.excep.enume.ErrorCode;
import com.pan.excep.error.ServiceException;
import com.pan.tools.RedisTools;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Aspect
@Component
public class StockAspect {

    @Resource
    private RedisTools redisTools;

    @Pointcut(value = "execution(public * com.pan.service.impl.StockServiceImpl.*(String,..))")
    public void stockAllMethod(){}

    @Before("stockAllMethod()")
    public void checkLogin(JoinPoint joinPoint){
        Object[] obj =  joinPoint.getArgs();
        String adminTicket = (String) obj[0];
        Result<Boolean> isAdmin = redisTools.exists("admin:"+adminTicket);
        if (isAdmin.getCode()!=200||!isAdmin.getData()){
            throw new ServiceException(ErrorCode.NOT_LOGIN);
        }
    }
}
